﻿
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace YanHongCrawler
{
    internal class SearchController
    {
        public string Download(string keyWord)
        {
            string url = GetUri(keyWord);

            WebClient client = new WebClient();
            client.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36 Edg/100.0.1185.50");
            byte[] pageData = client.DownloadData(url);
            string pageHtml = Encoding.Default.GetString(pageData);
            Console.WriteLine(pageHtml);
            return getChinese(pageHtml);
        }

        private string getChinese(string input)
        {
            string text = "";
            Regex reg = new Regex("[\u4e00-\u9fa5]+");
            foreach (Match v in reg.Matches(input))
            {
                text += v.ToString();
                if (text.Length > 200)
                {
                    text = text.Substring(0, 200);
                    break;
                }
            }
            return text;
        }

        private string GetUri(string keyWord)
        {
            string returnStr = String.Format("https://www.baidu.com/s?wd={0}", keyWord);
            return returnStr;
        }
    }
}
